package com.dataimport.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.TeacherEducationReformInfo;
import com.dataimport.entity.targetentity.EducationProject;
import com.dataimport.entity.targetentity.TeacherEducationProject;
import com.dataimport.mapper.EducationProjectMapper;
import com.dataimport.mapper.TeacherEducationReformInfoMapper;
import com.dataimport.service.EducationProjectService;
import com.dataimport.service.TeacherEducationProjectService;
import com.dataimport.utils.HasNullFieldUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 卑微小峰
 * @date 2022/9/27
 */
@Service
public class EducationProjectServiceImpl extends ServiceImpl<EducationProjectMapper, EducationProject> implements EducationProjectService {

    private static long PAGE_SIZE = 1000;

    @Autowired
    private TeacherEducationReformInfoMapper teacherEducationReformInfoMapper;

    @Autowired
    private TeacherEducationProjectService teacherEducationProjectService;

    @Override
    public void dealWithEducationProject() {

        long currentPage = 0L;
        while (true) {
            currentPage++;
            Map<String, Object> map = selectData(currentPage);
            List<EducationProject> educationProjectList = (List<EducationProject>) map.get("educationProjectList");
            List<TeacherEducationProject> teacherEducationProjectList = (List<TeacherEducationProject>) map.get("teacherEducationProjectList");

            if (ObjectUtils.isEmpty(educationProjectList)) {
                break;
            }

            insertData(educationProjectList, teacherEducationProjectList);
            System.out.println("导入成功");
        }

    }

    public Map<String, Object> selectData(long currentPage) {
        DynamicDataSourceContextHolder.push("mysql2");
        IPage<TeacherEducationReformInfo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(PAGE_SIZE);
        IPage<TeacherEducationReformInfo> data = teacherEducationReformInfoMapper.selectPage(page, null);
        DynamicDataSourceContextHolder.poll();

        Map<String, Object> educationProjectMap = new HashMap<>();
        List<TeacherEducationProject> teacherEducationProjectList = new ArrayList();

        List<TeacherEducationReformInfo> teacherEducationReformInfoList = data.getRecords();
        List<EducationProject> educationProjectList = teacherEducationReformInfoList.stream()
                .filter(teacherEducationReformInfo -> HasNullFieldUtil.inExistenceNull(teacherEducationReformInfo))
                .map(teacherEducationReformInfo -> {
                    String[] teacherCodeArr = teacherEducationReformInfo.getTeammateCode().split(",");
                    Set<String> set = new HashSet<>();
                    Collections.addAll(set, teacherCodeArr);

                    List<String> teacherCodeList = new ArrayList<>();
                    teacherCodeList.addAll(set);

                    for (int i = 0; i < teacherCodeList.size(); i++) {
                        TeacherEducationProject teacherEducationProject = new TeacherEducationProject();
                        teacherEducationProject.setEducationProjectCode(teacherEducationReformInfo.getEducationProjectCode());
                        teacherEducationProject.setTeacherCode(teacherCodeList.get(i));
                        teacherEducationProjectList.add(teacherEducationProject);
                    }

                    EducationProject educationProject = new EducationProject();
                    BeanUtils.copyProperties(teacherEducationReformInfo, educationProject);
                    educationProject.setStartProjectTime(DateUtil.parse(teacherEducationReformInfo.getLxsj()));
                    educationProject.setEndProjectTime(DateUtil.parse(teacherEducationReformInfo.getJxsj()));
                    return educationProject;
                })
                .collect(Collectors.toList());


        educationProjectMap.put("educationProjectList", educationProjectList);
        educationProjectMap.put("teacherEducationProjectList", teacherEducationProjectList);

        return educationProjectMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertData(List<EducationProject> educationProjectList, List<TeacherEducationProject> teacherEducationProjectList) {
        try {
            DynamicDataSourceContextHolder.push("mysql1");
            this.saveBatch(educationProjectList, educationProjectList.size());
            teacherEducationProjectService.saveBatch(teacherEducationProjectList, teacherEducationProjectList.size());
            DynamicDataSourceContextHolder.poll();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
